//
//  CZDetailHeaderView.swift
//  GZWeibo666
//
//  Created by apple on 15/12/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
// MARK: - 按钮点击代理
protocol CZDetailHeaderViewDelegate: NSObjectProtocol {
  
    func detailHeaderViewClick(headerView: UIView)
}


class CZDetailHeaderView: UIView {
    
    // MARK: - 属性
    /// 代理
        weak var delegate: CZDetailHeaderViewDelegate?
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            setButton(commentButton, title: "评论", count: status!.comments_count)
        }
    }
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, 356, 44))
        
        
        backgroundColor = UIColor(white: 0.99, alpha: 1)
        
        prepareUI()
    }
    
    // MARK: - 设置按钮文字
    private func setButton(button: UIButton, title: String, count: Int) {
        if count >= 10000 {
            let final = CGFloat(count) / 10000.0
            var titleText = String(format: "%.1f万", arguments: [final])
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            button.setTitle(title + " " + titleText, forState: UIControlState.Normal)
        } else {
            button.setTitle("\(title) \(count)", forState: UIControlState.Normal)
        }
    }
    
    
    // MARK: - 按钮点击事件
    func buttonClick(button: UIButton) {
        // 通知代理
        delegate?.detailHeaderViewClick(button)
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加点击事件
        commentButton.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 添加子控件
        addSubview(commentButton)
        addSubview(hintView)
        
        // 添加约束
        // 评论按钮
        commentButton.ff_AlignInner(type: ff_AlignType.CenterLeft, referView: self, size: CGSize(width: 70, height: 44), offset: CGPoint(x: 10, y: 0))
        
        // 三角型
        hintView.translatesAutoresizingMaskIntoConstraints = false
        hintView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: commentButton, size: nil)
    }
    
    // MARK: - 懒加载
    /// 评论
    private lazy var commentButton: UIButton = {
        let button = UIButton()
        button.setTitle("评论", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(11)
        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Highlighted)
        return button
        }()
    
    /// 三角形
    private lazy var hintView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "statusdetail_segmented_bottom_arrow"))
        return imageView
        }()
}

